package me;

public class TestApplication {
	
	private static final String EOF = "EOF";
	
	public static void test() {
		RecordBuffer buffer = new RecordBuffer();
		RecordProducer producer = new RecordProducer(buffer);
		RecordConsumer consumer = new RecordConsumer(buffer);
		
		produceThreadRun(producer);
		consumeThreadRun(consumer);
		
	}
	
	private static void produceThreadRun(RecordProducer producer) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i=0;i<10;i++) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					producer.send(String.valueOf(i));
				}
				producer.send(EOF);
			}
		},"producer").start();
	}
	
	private static void consumeThreadRun(RecordConsumer consumer) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				String record = null;
				while((record=(String)consumer.receive())!=EOF) {
					System.out.println(Thread.currentThread().getName() + "#" + record);
				}
				consumer.send(EOF);
			}
		},"consumer-1").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				String record = null;
				while((record=(String)consumer.receive())!=EOF) {
					System.out.println(Thread.currentThread().getName() + "#" + record);
				}
				consumer.send(EOF);
			}
		},"consumer-2").start();
	}
	
	public static void main(String[] args) {
		test();
	}
}
